package my.common;

import java.io.IOException;

/**
 * @author fengbo
 */
public class Errors {

    private static final String[] ERRORS = new String[512];

    static {
        for (int i = 0; i < ERRORS.length; i++) {
            // This is ok as strerror returns 'Unknown error i' when the message is not known.
            ERRORS[i] = strError(i);
        }
    }

    public static final class NativeIoException extends IOException {
        private static final long serialVersionUID = 1L;
        private final int expectedErr;
        NativeIoException(String method, int expectedErr) {
            super(method + "(..) failed: " + ERRORS[-expectedErr]);
            this.expectedErr = expectedErr;
        }

        public int expectedErr() {
            return expectedErr;
        }
    }

    public static NativeIoException newIOException(String method, int err) {
        return new NativeIoException(method, err);
    }

    private static native String strError(int err);
}
